// @author: caoshi
// @date: 2021-06-10 20:52
// 自定义中间件
package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

// 中间件m1 统计请求处理函数耗费的时间
func m1(c *gin.Context) {
	fmt.Println("m1 in ...")
	// 计时
	start := time.Now()
	c.Next() // 调用后续的处理函数

	cost := time.Since(start)
	fmt.Println(cost)
}

func indexHandler(c *gin.Context) {
	fmt.Println("indexHandler")
	c.JSON(http.StatusOK, gin.H{
		"msg": "index",
	})
}

func main() {
	router := gin.Default()
	router.GET("/index", m1, indexHandler)

	_ = router.Run(":8090")
}
